package com.shan.domm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shan.domm.common.ErrorCode;
import com.shan.domm.exception.BusinessException;
import com.shan.domm.mapper.RoomMapper;
import com.shan.domm.mapper.UserMapper;
import com.shan.domm.model.domain.Apply;
import com.shan.domm.model.domain.Room;
import com.shan.domm.model.domain.User;
import com.shan.domm.service.ApplyService;
import com.shan.domm.mapper.ApplyMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* @author 23949
* @description 针对表【apply(申请表)】的数据库操作Service实现
* @createDate 2024-05-16 07:59:18
*/
@Service
public class ApplyServiceImpl extends ServiceImpl<ApplyMapper, Apply>
    implements ApplyService{

    @Resource
    private ApplyMapper applyMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private RoomMapper roomMapper;

    @Override
    public String deleteApply(Apply apply) {
        Integer id = apply.getId();
        QueryWrapper queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        Long l = applyMapper.selectCount(queryWrapper);
        if(l<1){
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        int delete = applyMapper.delete(queryWrapper);
        if(delete>0){
            return "删除成功";
        }
        return "删除失败";
    }

    @Override
    public String AddApply(Apply apply) {
        boolean save = this.save(apply);
        if(!save){
            return "添加失败";
        }
        return "添加成功";

    }

    @Override
    public String agree(Apply apply) {
        Integer id=apply.getId();
        QueryWrapper<Apply> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        Long l = applyMapper.selectCount(queryWrapper);
        if(l<1){
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        apply.setState("已通过");
        boolean update = this.update(apply, queryWrapper);
//        String fromDorm = apply.getFromDorm();
        String toDorm = apply.getToDorm();
//        QueryWrapper<Room> queryWrapper1=new QueryWrapper<>();
//        queryWrapper1.eq("dorm_number",fromDorm);
//        Room room = roomMapper.selectOne(queryWrapper1);
//        if(room==null){
//            throw new BusinessException(ErrorCode.PARAMS_ERROR,"房间不存在");
//        }
//        room.setDormSnumber(room.getDormSnumber()-1);
//        int i = roomMapper.updateById(room);
//        if(i<1){
//            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
//        }
        QueryWrapper<Room> queryWrapper2=new QueryWrapper<>();
        queryWrapper2.eq("dorm_number",toDorm);
        Room room1 = roomMapper.selectOne(queryWrapper2);
        room1.setDormSnumber(room1.getDormSnumber()+1);
        int i1 = roomMapper.updateById(room1);
        if(i1<1){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }
        return "添加成功";
    }

    @Override
    public String refuse(Apply apply) {
        Integer id=apply.getId();
        int i = applyMapper.deleteById(id);
        if(i>0) {
            return "未通过";
        }
        return "未通过";
    }
}




